package hcs.ops.func;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class Hcs_Admission {

	
	public void admin(WebDriver driver, String pin) throws Exception{

		driver.manage().window().maximize();
		driver.findElement(By.xpath("//a[@href='/admission/home.html']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.findElement(By.xpath("//input[@name='param']")).sendKeys(pin);
		driver.findElement(By.xpath("//input[@name='action']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		
		List<WebElement> rows = driver.findElements(By.xpath("//table[@id='results']/tbody/tr"));
		int size = rows.size();
		
		try{
			String text = driver.findElement(By.xpath("//table[@id='results']/tbody/tr/td")).getText();
			if(text.equals("NO PATIENT FOUND")){
				System.out.println("0 matches found in Admission");
			}else{
				System.out.println(size +" matches found in search in Admission");
			}
		}catch(Exception e){
			System.out.println("0 matches found in Admission");
		}
		
		driver.findElement(By.xpath("//table[@id='results']/tbody/tr/td[10]/div[2]/a")).click();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		String mainWindow = driver.getWindowHandle();
		driver.switchTo().window(mainWindow);
		
		//admit patient
		Select account = new Select(driver.findElement(By.xpath("//select[@id='accountClass']")));
		account.selectByVisibleText("INDIVIDUAL");
		Select room = new Select(driver.findElement(By.xpath("//select[@id='roomChargeCode']")));
		room.selectByVisibleText("REGULAR PRIVATE");
		driver.findElement(By.xpath("//input[@id='nursingUnitCode']")).clear();
		driver.findElement(By.xpath("//input[@id='nursingUnitCode']")).sendKeys("0287");
		
		//ROOM AND BED CODE GOES HERE
		driver.findElement(By.xpath("//input[@id='roomNoFinder']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		Actions actions = new Actions(driver);
        WebElement mouseover1 = driver.findElement(By.xpath("//div[@id='roomBedFinderForm']/div[2]/div[1]/input[1]"));
        actions.moveToElement(mouseover1);
        actions.click().perform();
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
        
        List<WebElement> row = driver.findElements(By.xpath("//tbody[@id='rbf_finder_table_body']/tr"));
		int sizes = row.size();
        System.out.println(sizes);
        driver.findElement(By.xpath("//tbody[@id='rbf_finder_table_body']/tr[1]/td[1]/a")).click();
        
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
		driver.findElement(By.xpath("//input[@id='diagnosisCode']")).sendKeys("010000");
		driver.findElement(By.xpath("//input[@id='doctorCode']")).sendKeys("5825");
		driver.findElement(By.xpath("//input[@id='guarantorTelNo']")).sendKeys("12345");
		
		//button preview admission
		driver.findElement(By.xpath("//input[@value='Preview']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

		//proceed button code goes here
		//driver.findElement(By.xpath("//span[@class='ui-button-text' and contains(., 'Proceed')]")).click();
		driver.findElement(By.xpath("//div[@class='ui-dialog-buttonset']/button[1]")).click();
		
		//save admission
		driver.findElement(By.xpath("//input[@value='Save Admission']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
	}
	
	public void history(WebDriver driver, String pin) throws Exception{
		
		driver.manage().window().maximize();
		driver.findElement(By.xpath("//a[@href='/admission/home.html']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.findElement(By.xpath("//input[@name='param']")).sendKeys(pin);
		driver.findElement(By.xpath("//input[@name='action']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		List<WebElement> rows = driver.findElements(By.xpath("//table[@id='results']/tbody/tr"));
		int size = rows.size();
		
		try{
			String text = driver.findElement(By.xpath("//table[@id='results']/tbody/tr/td")).getText();
			if(text.equals("NO PATIENT FOUND")){
				System.out.println("0 matches found in Admission");
			}else{
				System.out.println(size +" matches found in search in Admission");
			}
		}catch(Exception e){
			System.out.println("0 matches found in Admission");
		}
		
		driver.findElement(By.xpath("//table[@id='results']/tbody/tr/td[10]/div[3]/a")).click();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		
	}
	
	public void update_admission(WebDriver driver, String pin) throws Exception{
		
		driver.manage().window().maximize();
		driver.findElement(By.xpath("//a[@href='/admission/home.html']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		driver.findElement(By.xpath("//input[@name='param']")).sendKeys(pin);
		driver.findElement(By.xpath("//input[@name='action']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		List<WebElement> rows = driver.findElements(By.xpath("//table[@id='results']/tbody/tr"));
		int size = rows.size();
		
		try{
			String text = driver.findElement(By.xpath("//table[@id='results']/tbody/tr/td")).getText();
			if(text.equals("NO PATIENT FOUND")){
				System.out.println("0 matches found in Admission");
			}else{
				System.out.println(size +" matches found in search in Admission");
			}
		}catch(Exception e){
			System.out.println("0 matches found in Admission");
		}
		
		driver.findElement(By.xpath("//table[@id='results']/tbody/tr/td[10]/div[2]/a")).click();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		String mainWindow = driver.getWindowHandle();
		driver.switchTo().window(mainWindow);
		
		driver.findElement(By.xpath("//input[@id='onQueue']")).click();
		
		Select mobilization = new Select(driver.findElement(By.xpath("//select[@id='mobilizationTypeCode']")));
		mobilization.selectByVisibleText("WALK-IN");
		
		//ROOM AND BED CODE GOES HERE
		driver.findElement(By.xpath("//input[@id='roomNoFinder']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		Actions actions = new Actions(driver);
		WebElement mouseover1 = driver.findElement(By.xpath("//div[@id='roomBedFinderForm']/div[2]/div[1]/input[1]"));
		actions.moveToElement(mouseover1);
		actions.click().perform();
		driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
		
		List<WebElement> row = driver.findElements(By.xpath("//tbody[@id='rbf_finder_table_body']/tr"));
		int sizes = row.size();
        System.out.println(sizes);
        driver.findElement(By.xpath("//tbody[@id='rbf_finder_table_body']/tr[1]/td[1]/a")).click();
		
		driver.findElement(By.xpath("//input[@id='guarantorAddress']")).sendKeys("Manila");
		driver.findElement(By.xpath("//input[@id='guarantorTelNo']")).sendKeys("12345");
		
		//button preview admission
		driver.findElement(By.xpath("//input[@value='Preview']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

		//proceed button code goes here
		//driver.findElement(By.xpath("//span[@class='ui-button-text' and contains(., 'Proceed')]")).click();
		driver.findElement(By.xpath("//div[@class='ui-dialog-buttonset']/button[1]")).click();
				
		//save admission
		driver.findElement(By.xpath("//input[@value='Save Admission']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
				
	}
	
}
